<?php

use yii\helpers\Html;
use yii\widgets\DetailView;

use yii\helpers\Url;
use app\models\U;

use app\models\MGoods;
//use yii\jui\DatePicker;
use kartik\date\DatePicker;

/* @var $this yii\web\View */
/* @var $model app\models\MGoods */

//$this->title = $model->title;
//$this->params['breadcrumbs'][] = ['label' => '商品管理', 'url' => ['index', 'pub_userid' => $model->pub_userid]];
//$this->params['breadcrumbs'][] = $this->title;

$this->title = $model->goods_id;
$this->params['breadcrumbs'][] = ['label' => '商品列表', 'url' => ['client-pubgoods-list',  'pub_type' =>$model->pub_type, 'goods_neworold' =>$model->goods_neworold]];
$this->params['breadcrumbs'][] = "商品编号#".$this->title;

if($model->pub_type == 1)
{
    $pub_hint="租赁";
    $pub_badge="<span class=\"label label-primary\">租赁</span>";
}
else
{
    $pub_hint="购买";
    if($model->goods_neworold ==1)
    {
        $pub_badge="<span class=\"label label-success\">出售-新品</span>";
    }
    else
    {
        $pub_badge="<span class=\"label label-warning\">出售-二手</span>";
    }
}
?>

<style type="text/css">
    img {
        width: 100%;
    }
</style>

<div class="mgoods-view">

    <h4><?= Html::encode($model->title) ?>  | <?=$pub_badge?></h4>

    <img style="width: 55% " src='<?= $model->list_img_url?>' >

    <!-- Large modal -->
    <br>
    <button type="button" class="btn btn-success btn-lg btn-block" data-toggle="modal" data-target=".bs-example-modal-lg">我要<?= $pub_hint ?></button>
    <br>
    <br>
    <div class="alert alert-danger" role="alert" id="hint">
        <?= Html::a('请先登录 >>', ['site/login']) ?>
    </div>
    <div class="alert alert-success" role="alert" id="order">
        申请订单已生成  &nbsp;&nbsp;<?= Html::a('我的订单 >>', ['order/index','userid'=>Yii::$app->user->identity->id]) ?>
    </div>
    <br>
    <?= DetailView::widget([
        'model' => $model,
        //'options' => ['class' => 'table table-striped table-bordered detail-view'],
        'template' => '<tr><th width=30%>{label}</th><td>{value}</td></tr>',
        'options' => ['class' => 'table table-striped detail-view'],
        'attributes' => [
            //'goods_id',
            /*
            [
                'label' => '商品小图',
                'value' => '<img src=' . $model->list_img_url . '>',
                'format'=> 'html',
            ],
            */

            'title',
            /*
            [
                'label' => '发布者',
                'value' => $model->getPubUserName($model),
                'format'=> 'html',
            ],
         
            [
                'label' => '发布状态',
                'value' => $model->getStatus($model),
                'format'=> 'html',
            ],
               */
            [
                'label' => '一级分类',
                'value' => $model->getKindName($model->goods_kind),
                'format'=> 'html',
            ],
            [
                'label' => '二级分类',
                'value' => $model->getKindName($model->goods_subkind),
                'format'=> 'html',
            ],
            [
                'label' => '三级分类',
                'value' => $model->getKindName($model->goods_3rdkind),
                'format'=> 'html',
            ],

            [
                'label' => '发布方式',
                'value' => $model->getPubType($model),
                'format'=> 'html',
            ],

            [
                'label' => '商品成色',
                'value' => $model->getGoodsNeworold($model),
                'format'=> 'html',
            ],

            //'parameter',
            [
                'label' => '参数',
                'value' => $model->parameter,
                'format'=> 'html',
            ],

            'attachment',

            //'brand',
            [
                'label' => '品牌',
                'value' => $model->getBrand($model),
                'format'=> 'html',
            ],

            'model',
            'prod_area',
            'descript',
            //'price',
            //'price_hint',
            //'price_old',
            //'list_img_url:url',


            /*
  
            //'body_img_url:url',
            
            [
                'label' => '商品大图',
                'value' => $model->getViewGoodsPics($model),
                'format'=> 'html',
            ],
            */

            //'detail:ntext',
            /*
            [
                'attribute' => 'detail',
                'format'=> 'html',
            ],
            */


            //'quantity',
        ],
    ]) ?>

    <?= $model->detail ?>
    <center>
    <!--
    <button type="button" class="btn btn-success btn-lg" id="zujie" style="height:50px;width:160px">我要租借</button>
    -->
        <!--
    <button type="button" class="btn btn-success btn-lg btn-block" id="zujie">我要租借</button>
    -->
    <!--
    &nbsp;&nbsp;
    <button type="button" class="btn btn-danger btn-lg" id="dyj100" style="height:80px">100元代金券 (-10000分)</button>
    -->
    </center>


</div>


<div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" id="orderModal">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="gridSystemModalLabel">我要<?= $pub_hint ?></h4>
            </div>
            <div class="modal-body">
                <?php
                    if(!(Yii::$app->user->isGuest)) {
                ?>
                <h4>商品信息</h4>
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-md-4">
                            <div class="form-group field-mgoods-pic">
                                <label class="control-label" for="modal-pic"></label>
                                <P><img src='<?= $model->list_img_url ?>' style="width:120px; height:80px"></P>
                                <div class="help-block"></div>
                            </div>
                        </div>
                        <div class="col-md-8">
                            <div class="form-group field-mgoods-title">
                                <label class="control-label" for="modal-title">名称</label>
                                <P><?= $model->title ?> </P>
                                <div class="help-block"></div>
                            </div>
                            <div class="form-group field-mgoods-address">
                                <label class="control-label" for="modal-address">型号</label>
                                <P><?= $model->model ?> </P>
                                <div class="help-block"></div>
                            </div>
                        </div>
                        <hr>
                    </div>

                <!--
                <h4>租借人信息</h4>
                <div class="form-group field-mgoods-mobile">
                    <input type="text" id="modal-mobile" class="form-control" name="modal-mobile" maxlength="11" placeholder="联系电话">
                    <div class="help-block"></div>
                </div>
                <div class="form-group field-mgoods-address">
                    <input type="text" id="modal-address" class="form-control" name="modal-address" maxlength="128" placeholder="地址">
                    <div class="help-block"></div>
                </div>
                 -->

                <h4><?= $pub_hint ?>情况</h4>
                <div class="form-group field-mgoods-count">
                    <input type="text" id="modal-count" class="form-control" name="modal-count" maxlength="128" placeholder="数量">
                    <div class="help-block"></div>
                </div>

                <div class="form-group field-mgoods-starttime">
                    <!--
                    <input type="text" id="modal-starttime" class="form-control" name="modal-starttime" maxlength="128" placeholder="租借时间">
                    -->
                    <?
                    echo '<label>租借时间</label><br><br>';
                    echo DatePicker::widget([
                        'name' => 'modal-starttime',
                        'id' => 'modal-starttime',
                        //'language' => 'zh',
                        'options' => ['placeholder' => '租借时间 ...'],
                        'pluginOptions' => [
                            'format' => 'mm/dd/yyyy',
                            'autoclose' => true,
                            //'todayBtn' => true,
                            'todayHighlight' => true
                        ]
                    ]);
                    ?>
                    <div class="help-block"></div>
                </div>
                <div class="form-group field-mgoods-endtime">
                    <?
                    echo '<label>归还时间</label><br><br>';
                    echo DatePicker::widget([
                        'name' => 'modal-endtime',
                        'id' => 'modal-endtime',
                        //'language' => 'zh',
                        'options' => ['placeholder' => '归还时间 ...'],
                        'pluginOptions' => [
                            'format' => 'mm/dd/yyyy',
                            'autoclose' => true,
                            //'todayBtn' => true,
                            'todayHighlight' => true
                        ]
                    ]);
                    ?>
                    <div class="help-block"></div>
                </div>

                <h4>收货人信息</h4>
                <div class="form-group field-mgoods-receiver_name">
                    <input type="text" id="modal-receiver_name" class="form-control" name="modal-receiver_name" maxlength="32" placeholder="收货人姓名">
                    <div class="help-block"></div>
                </div>

                <div class="form-group field-mgoods-receiver_address">
                    <input type="text" id="modal-receiver_address" class="form-control" name="modal-receiver_address" maxlength="128" placeholder="收货人地址">
                    <div class="help-block"></div>
                </div>

                <div class="form-group field-mgoods-receiver_mobile">
                    <input type="text" id="modal-receiver_mobile" class="form-control" name="modal-receiver_mobile" maxlength="16" placeholder="收货人电话">
                    <div class="help-block"></div>
                </div>

                <div class="form-group field-mgoods-receiver_memo">
                    <input type="text" id="modal-receiver_memo" class="form-control" name="modal-receiver_memo" maxlength="32" placeholder="收货人备注">
                    <div class="help-block"></div>
                </div>


                <h4>开票信息</h4>

                <div class="form-group field-mgoods-invoice_type">
                    <label class="control-label" for="modal-invoice_type">发票</label>
                    <select id="modal-invoice_type" class="form-control" name="modal-invoice_type">
                        <option value="1" selected>不需要发票</option>
                        <option value="2">增值税普通发票（租金加17% ）</option>
                        <option value="3">增值税发票（租金加17%）</option>
                    </select>

                    <div class="help-block"></div>
                </div>

                <div class="form-group field-mgoods-invoice_company">
                    <input type="text" id="modal-invoice_company" class="form-control" name="modal-invoice_company" maxlength="32" placeholder="单位名称">
                    <div class="help-block"></div>
                </div>
                <div class="form-group field-mgoods-invoice_bank">
                    <input type="text" id="modal-invoice_bank" class="form-control" name="modal-invoice_bank" maxlength="32" placeholder="开户行">
                    <div class="help-block"></div>
                </div>

                <div class="form-group field-mgoods-invoice_receivernumber">
                    <input type="text" id="modal-invoice_receivernumber" class="form-control" name="modal-invoice_receivernumber" maxlength="32" placeholder="税号">
                    <div class="help-block"></div>
                </div>
                <div class="form-group field-mgoods-invoice_address">
                    <input type="text" id="modal-invoice_address" class="form-control" name="modal-invoice_address" maxlength="32" placeholder="地址">
                    <div class="help-block"></div>
                </div>
                <div class="form-group field-mgoods-invoice_mobile">
                    <input type="text" id="modal-invoice_mobile" class="form-control" name="modal-invoice_mobile" maxlength="32" placeholder="电话">
                    <div class="help-block"></div>
                </div>

                <div class="form-group field-mgoods-invoice_memo">
                    <input type="text" id="modal-invoice_memo" class="form-control" name="modal-invoice_memo" maxlength="32" placeholder="备注">
                    <div class="help-block"></div>
                </div>

                <?php } else { ?>
                        <h4>请先<a href='<?= \yii\helpers\Url::to(['site/login'], true) ; ?>'>登录</a>！</h4>
                <?php } ?>

                </div><!-- endof modal-body-->

            <div class="modal-footer">
                <?php
                    $rxdh = \app\models\Keywords::findOne(['key'=>'rxdh']);
                    $rxdh = $rxdh->value;
                ?>
                <span class="pull-left">
                    <a href="tel:<?= $rxdh ?>" style="font-size: 12pt;color: #ee7e20;">
                    免费咨询&nbsp;&nbsp;<?= $rxdh ?>
                    </a>
                </span>


                <button type="button" class="btn btn-primary" id="zujie2">确定</button>
            </div>
        </div><!-- endof modal-content-->
        </div>
    </div>
</div>

<script src="js/jquery.min.js"></script>
<script type="text/javascript">
    var pub_type="<?= $model->pub_type ?>";

    $("#hint").hide();
    $("#order").hide();

    $("#modal-invoice_company").hide();
    $("#modal-invoice_bank").hide();
    $("#modal-invoice_receivernumber").hide();
    $("#modal-invoice_address").hide();
    $("#modal-invoice_mobile").hide();
    $("#modal-invoice_memo").hide();


     var isGuest = '<?= (Yii::$app->user->isGuest)?1:0 ?>';


    $(document).ready(function() {

            /*如果是购买，隐藏租借开始和结束时间输入框*/
            if(parseInt(pub_type) == 2)
            {
                $(".field-mgoods-starttime").hide();
                $(".field-mgoods-endtime").hide();
            }

            $('#modal-invoice_type').change (function () {
                if($("#modal-invoice_type").val() != 1)
                {
                    $("#modal-invoice_company").show();
                    $("#modal-invoice_bank").show();
                    $("#modal-invoice_receivernumber").show();
                    $("#modal-invoice_address").show();
                    $("#modal-invoice_mobile").show();
                    $("#modal-invoice_memo").show();
                }
                else
                {
                    $("#modal-invoice_company").hide();
                    $("#modal-invoice_bank").hide();
                    $("#modal-invoice_receivernumber").hide();
                    $("#modal-invoice_address").hide();
                    $("#modal-invoice_mobile").hide();
                    $("#modal-invoice_memo").hide();
                }
            });

        
            $('#zujie').click (function () {

                //alert('confirmAjax');
                // if (!confirm("确定要租借吗?"))
                //   return;

                if(isGuest == 1)
                {
                    //alert("you are guest");
                    //$("#hint").show();
                    location.href = '<?php echo Url::to(["site/login"]); ?>';
                    return false;
                }
                else
                {
                    var user_id = '<?= Yii::$app->user->identity->id ?>';
                }

                var args = {
                    'classname':    '\\app\\models\\MGoods',
                    'funcname':     'zujieAjax',
                    'params':       {
                        'goods_id': '<?= $model->goods_id ?>',    
                        'user_id': user_id,
                    }
                };
                $.ajax({
                    url:        "<?= \yii\helpers\Url::to(['site/siteajax'], true) ; ?>",
                    type:       "GET",
                    cache:      false,
                    dataType:   "json",
                    data:       "args=" + JSON.stringify(args),
                    success:    function(ret) { 
                        if (0 === ret['code']) 
                        {
                            //alert("租借成功！");
                            //location.href = '<?= Url::to() ?>';
                            $("#order").show();
                        } 
                        else
                        {
                             alert("error");
                        }
                    },                        
                    error:      function(){
                        alert('发送失败。');
                    }
                });
            });

        /*-----------------------------------------------------------------------------------------------------*/

        $('#zujie2').click (function () {

            if(isGuest == 1)
            {
                //alert("you are guest");
                //$("#hint").show();
                location.href = '<?php echo Url::to(["site/login"]); ?>';
                return false;
            }
            else
            {
                var user_id = '<?= Yii::$app->user->identity->id ?>';
            }

            var args = {
                'classname':    '\\app\\models\\MGoods',
                'funcname':     'zujie2Ajax',
                'params':       {
                    'goods_id': '<?= $model->goods_id ?>',
                    'user_id': user_id,
                    'count': $("#modal-count").val(),
                    'starttime': $("#modal-starttime").val(),
                    'endtime': $("#modal-endtime").val(),
                    'receiver_name': $("#modal-receiver_name").val(),
                    'receiver_address': $("#modal-receiver_address").val(),
                    'receiver_mobile': $("#modal-receiver_mobile").val(),
                    'receiver_memo': $("#modal-receiver_memo").val(),
                    'invoice_type': $("#modal-invoice_type").val(),
                    'invoice_company': $("#modal-invoice_company").val(),
                    'invoice_bank': $("#modal-invoice_bank").val(),
                    'invoice_receivernumber': $("#modal-invoice_receivernumber").val(),
                    'invoice_address': $("#modal-invoice_address").val(),
                    'invoice_mobile': $("#modal-invoice_mobile").val(),
                    'invoice_memo': $("#modal-invoice_memo").val(),
                }
            };
            $.ajax({
                url:        "<?= \yii\helpers\Url::to(['site/siteajax'], true) ; ?>",
                type:       "GET",
                cache:      false,
                dataType:   "json",
                data:       "args=" + JSON.stringify(args),
                success:    function(ret) {
                    if (0 === ret['code'])
                    {
                        //alert("租借成功！");
                        //location.href = '<//?= Url::to() ?>';
                        $('#orderModal').modal('hide');
                        $("#order").show();
                    }
                    else
                    {
                        alert("error");
                    }
                },
                error:      function(){
                    alert('发送失败。');
                }
            });
        });


    });
</script>
